// === Level.java ===
package com.cardgame.model.level;

import com.cardgame.dao.ArchivalDao;
import com.cardgame.dao.BuffDao;
import com.cardgame.dao.CardDao;
import com.cardgame.dao.RoleDao;
import com.cardgame.model.card.ActionCard;
import com.cardgame.model.role.Skill;
import lombok.Getter;

import java.util.*;

@Getter
public class Level {
    private LevelPlayer selfPlayer;
    private LevelPlayer enemyPlayer;
    private int Round;

    private final CardDao cardDao;
    private final RoleDao roleDao;
    private final ArchivalDao archivalDao;
    private final BuffDao buffDao;

    private boolean selfSkillUsedThisTurn = false;

    public Level(CardDao cardDao, RoleDao roleDao, ArchivalDao archivalDao, BuffDao buffDao) {
        this.cardDao = cardDao;
        this.roleDao = roleDao;
        this.archivalDao = archivalDao;
        this.buffDao = buffDao;
        this.selfPlayer = null;
        this.enemyPlayer = null;
        this.Round = 1;
    }

    /**
     * 初始化战斗状态：根据传入卡组ID自动设定主角、初始卡组与MP/HP。
     * 
     * @param selfDeckId  玩家卡组ID
     * @param enemyDeckId 敌方卡组ID
     */
    public void initMainRoles(int selfDeckId, int enemyDeckId) {
        selfPlayer = new LevelPlayer(archivalDao, cardDao, roleDao, buffDao, selfDeckId);
        enemyPlayer = new LevelPlayer(archivalDao, cardDao, roleDao, buffDao, enemyDeckId);
    }

    /**
     * 玩家抽牌：从抽牌堆中摸牌直到手牌数量为 5 或抽牌堆为空。
     */
    public void SelfDealCards() {
        selfPlayer.DealCards();
    }

    /**
     * 敌方抽牌：从敌方卡组中摸牌直到手牌数量为 5 或抽牌堆为空。
     */
    public void EnemyDealCards() {
        enemyPlayer.DealCards();
    }

    /**
     * 玩家使用卡牌。
     * 根据卡牌类型触发对应效果，如角色上场、装备武器、发动技能。
     * 使用成功后从手牌移除，并加入弃牌堆。
     * 
     * @param cardId 要使用的卡牌 ID
     */
    public void SelfUseCard(int cardId, int target) {
        Optional<ActionCard> optionalActionCard = selfPlayer.UseCard(cardId, target);
        if (optionalActionCard.isPresent()) {
            ActionCard actionCard = optionalActionCard.get();
            enemyPlayer.GetActionCardEffect(actionCard);
        }
    }

    public boolean SelfUseSkill() {
        if (selfSkillUsedThisTurn)
            return false;

        Optional<Skill> optionalSkill = selfPlayer.UseSkill();
        optionalSkill.ifPresent(skill -> enemyPlayer.GetSkillEffect(skill));

        selfSkillUsedThisTurn = true;
        return true;
    }

    /**
     * 玩家结束回合：触发所有己方角色的攻击行为。
     * 自动攻击敌方场上单位或主角。
     * 检查是否胜负结束，若未结束则重置 MP 并推进回合。
     * 
     * @return 1=胜利，-1=失败，0=继续游戏
     */
    public int SelfEndRound() {
        List<Integer> damageList = selfPlayer.SelfEndTurn();
        if (enemyPlayer.EnemyEndTurn(damageList))
            return 1;
        Round++;
        selfSkillUsedThisTurn = false;
        return 0;
    }

    /**
     * 敌方出牌逻辑：根据卡牌加权价值排序，优先打出高价值卡直到耗尽法力。
     */
    public void EnemyUseCard() {
        LevelPlayer.AIActionResult aiActionResult = enemyPlayer.AIAction();
        if (aiActionResult != null) {
            aiActionResult.getUsedCards().forEach(
                    optionalActionCard ->
                            optionalActionCard.ifPresent(
                                    actionCard ->
                                            enemyPlayer.GetActionCardEffect(actionCard)));
            aiActionResult.getUsedSkills().forEach(
                    optionalSkill ->
                            optionalSkill.ifPresent(
                                    skill -> enemyPlayer.GetSkillEffect(skill)));
        }
    }

    /**
     * 敌方结束回合：执行自动攻击。
     * 
     * @return 1=敌方胜利，-1=敌方失败，0=继续游戏
     */
    public int EnemyEndRound() {
        List<Integer> damageList = enemyPlayer.SelfEndTurn();
        if (selfPlayer.EnemyEndTurn(damageList))
            return 1;
        Round++;
        return 0;
    }
}
